package com.github.postsanf.yinian.adapter;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNUpList;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.service.UpLoadService;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.SizeFormat;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class UploadAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<YNUpList> datas;
    private ImageLoader imageLoader;
    private static final int EMPTY_VIEW = 10;
    private Resources mResource;

    public UploadAdapter(Context context, List<YNUpList> datas) {
        this.context = context;
        this.datas = datas;
        imageLoader = ImageLoader.getInstance();
    }

    public void setSource(Resources mResource) {
        this.mResource = mResource;
    }

    public class EmptyViewHolder extends RecyclerView.ViewHolder {
        public EmptyViewHolder(View itemView) {
            super(itemView);
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout rl_item_upload;                        //列表View
        ImageView iv_upload_avatar;                        //封面图
        TextView tv_upload_title;                        //标题
        TextView tv_upload_size;                        //大小
        TextView tv_upload_num;                            //张数
        TextView tv_upload_time;                        //时间
        CheckBox cb_upload_start_end;                    //开关
        ImageView iv_upload_del;
        ProgressBar pro_upp_progressBar;

        public ViewHolder(View itemView) {
            super(itemView);
            rl_item_upload = (RelativeLayout) itemView
                    .findViewById(R.id.rl_item_upload);
            iv_upload_avatar = (ImageView) itemView
                    .findViewById(R.id.iv_upload_avatar);
            tv_upload_title = (TextView) itemView
                    .findViewById(R.id.tv_upload_title);
            tv_upload_size = (TextView) itemView
                    .findViewById(R.id.tv_upload_size);
            tv_upload_num = (TextView) itemView
                    .findViewById(R.id.tv_upload_num);
            tv_upload_time = (TextView) itemView
                    .findViewById(R.id.tv_upload_time);
            cb_upload_start_end = (CheckBox) itemView
                    .findViewById(R.id.cb_upload_start_end);
            iv_upload_del = (ImageView) itemView
                    .findViewById(R.id.iv_upload_delete);
            pro_upp_progressBar = (ProgressBar) itemView
                    .findViewById(R.id.pro_upp_progressBar);
        }

        public void bindData(final YNUpList listItems) {
            if (listItems != null) {
                if (listItems.getUpimg().startsWith("http")) {
                    imageLoader.displayImage(listItems.getUpimg(), iv_upload_avatar,
                            ImageOptHelper.getCornerOptions(2));
                } else {
                    imageLoader.displayImage("file:///" + listItems.getUpimg(), iv_upload_avatar,
                            ImageOptHelper.getCornerOptions(2));
                }
                pro_upp_progressBar.setProgress(listItems.getUppercent());
                tv_upload_time.setText(listItems.getUptime());
                tv_upload_title.setText(listItems.getUpgntitle());
                tv_upload_num.setText(SizeFormat.formatSize(listItems.getUpsize()) + "/" + SizeFormat.formatSize(Long.parseLong(listItems.getUpTotalSize())));
//                cb_upload_start_end.setChecked(listItems.isCancel());
//                cb_upload_start_end.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        cb_upload_start_end.setChecked(!cb_upload_start_end.isChecked());
//                        listItems.setIsCancel(!listItems.isCancel());
//                        // 发送通知,切换上传状态
//                        Intent mIntent = new Intent(context, UpLoadService.class);
//                        mIntent.setAction(ACTIONs.actionCancelUpLoad);
//                        mIntent.putExtra(ACTIONs.aCancelId, listItems.getUpid());
//                        mIntent.putExtra(ACTIONs.aIsCancel, listItems.isCancel());
//                        context.startService(mIntent);
//                    }
//                });
                iv_upload_del.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent mIntent = new Intent(context, UpLoadService.class);
                        mIntent.setAction(ACTIONs.actionDeleteUpload);
                        mIntent.putExtra(ACTIONs.aCancelId, listItems.getUpid());
                        mIntent.putExtra(ACTIONs.aIsCancel, listItems.isCancel());
                        context.startService(mIntent);
                    }
                });
            }
        }
    }

    private YNUpList getItem(int position) {
        return datas.get(position);
    }

    // 创建ViewHolder
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == EMPTY_VIEW) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.yn_empty_upprogress, parent, false);
            v.getRootView().getLayoutParams().height = DisplayUtils.getScreenHeightPixels((Activity) context) - DisplayUtils.dp2px(context, 160);
            return new EmptyViewHolder(v);
        }

        View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.yn_item_upload, parent, false);
        return new ViewHolder(convertView);
    }

    // 绑定数据到View
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder rv_holder, int position) {
        if (rv_holder instanceof ViewHolder) {
            final ViewHolder holder = (ViewHolder) rv_holder;
            YNUpList listItems = getItem(position);
            holder.bindData(listItems);
            if (mResource != null) {
                holder.cb_upload_start_end.setBackground(mResource.getDrawable(mResource.getIdentifier("btn_upload_sel", CommonConstants.THEME_DRAWABLE, CommonConstants.THEME_PACKAGE)));
            }
            holder.itemView.setTag(listItems);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (datas.size() == 0) {
            return EMPTY_VIEW;
        }
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return datas.size() > 0 ? datas.size() : 1;
    }
}
